<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>奖励管理</title>
  <style>
    body {
      font-family: Arial, sans-serif;
      background-color: #f4f4f4;
      margin: 0;
      padding: 0;
    }
    .container {
      width: 80%;
      margin: 50px auto;
      padding: 20px;
      background: #fff;
      box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
    }
    h2 {
      text-align: center;
      color: #333;
    }
    form {
      display: flex;
      flex-direction: column;
    }
    label {
      margin-bottom: 5px;
      color: #555;
    }
    input[type="text"],
    input[type="number"],
    textarea,
    input[type="submit"] {
      padding: 10px;
      margin-bottom: 15px;
      border: 1px solid #ddd;
      border-radius: 4px;
    }
    input[type="submit"] {
      background-color: #28a745;
      color: white;
      border: none;
      cursor: pointer;
    }
    input[type="submit"]:hover {
      background-color: #218838;
    }
    table {
      width: 100%;
      border-collapse: collapse;
      margin-top: 20px;
    }
    th, td {
      padding: 12px;
      text-align: left;
      border-bottom: 1px solid #ddd;
    }
    th {
      background-color: #f2f2f2;
    }
    tr:hover {
      background-color: #f5f5f5;
    }
  </style>
</head>
<body>
<div class="container">
  <h2>奖励管理</h2>
  <button onclick="fetchRewards()">刷新奖励列表</button>
  <form id="addRewardForm">
    <label for="rewardName">奖励名称:</label>
    <input type="text" id="rewardName" name="rewardName" required>

    <label for="rewardDescription">奖励描述:</label>
    <textarea id="rewardDescription" name="rewardDescription" required></textarea>

    <label for="requiredPoints">所需积分:</label>
    <input type="number" id="requiredPoints" name="requiredPoints" required>

    <input type="submit" value="添加奖励">
  </form>

  <table id="rewardsTable">
    <thead>
    <tr>
      <th>奖励ID</th>
      <th>奖励名称</th>
      <th>奖励描述</th>
      <th>所需积分</th>
      <th>操作</th>
    </tr>
    </thead>
    <tbody>
    <!-- 奖励数据将在这里动态插入 -->
    </tbody>
  </table>
</div>

<script>
  // 获取所有奖励物品
  function fetchRewards() {
    fetch('http://localhost:8081/waste/api/rewards/getAll')
            .then(response => response.json())
            .then(rewards => {
              const tableBody = document.querySelector('#rewardsTable tbody');
              tableBody.innerHTML = ''; // 清空表格内容

              rewards.forEach(reward => {
                const row = document.createElement('tr');
                row.innerHTML = `
              <td>${reward.rewardId}</td>
              <td>${reward.rewardName}</td>
              <td>${reward.rewardDescription}</td>
              <td>${reward.requiredPoints}</td>
              <td>
                  <button onclick="editReward(${reward.rewardId})">编辑</button>
                  <button onclick="deleteReward(${reward.rewardId})">删除</button>
              </td>
          `;
                tableBody.appendChild(row);
              });
            })
            .catch(error => {
              alert('获取奖励数据失败，请检查网络连接！');
              console.error('Error:', error);
            });
  }

  // 添加奖励物品
  document.getElementById('addRewardForm').addEventListener('submit', function(event) {
    event.preventDefault();
    const formData = new FormData(this);
    const data = {
      rewardName: formData.get('rewardName'),
      rewardDescription: formData.get('rewardDescription'),
      requiredPoints: formData.get('requiredPoints')
    };

    fetch('http://localhost:8081/waste/api/rewards/add', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json'
      },
      body: JSON.stringify(data)
    })
            .then(response => response.json())
            .then(responseData => {
              if (responseData.success) {

                fetchRewards(); // 刷新奖励列表
              } else {

              }
            })
            .catch(error => {

            });
    alert('奖励物品添加成功！');
  });

  // 编辑奖励物品
  function editReward(rewardId) {
    const rewardName = prompt('请输入新的奖励名称:');
    const rewardDescription = prompt('请输入新的奖励描述:');
    const requiredPoints = prompt('请输入新的所需积分:');

    if (rewardName === null || rewardDescription === null || requiredPoints === null ||
            rewardName.trim() === '' || rewardDescription.trim() === '' || requiredPoints.trim() === '') {
      alert('请输入有效的奖励信息！');
      return;
    }

    const data = {
      rewardId: rewardId,
      rewardName: rewardName,
      rewardDescription: rewardDescription,
      requiredPoints: parseInt(requiredPoints, 10)
    };

    fetch('http://localhost:8081/waste/api/rewards/update', {
      method: 'PUT',
      headers: {
        'Content-Type': 'application/json'
      },
      body: JSON.stringify(data)
    })
            .then(response => response.json())
            .then(responseData => {
              if (responseData.success) {

                fetchRewards(); // 刷新奖励列表
              } else {

              }
            })
            .catch(error => {

            });
    alert('奖励物品修改成功！');
  }

  // 删除奖励物品
  function deleteReward(rewardId) {
    if (!confirm('确定要删除该奖励物品吗？')) {
      return;
    }

    fetch(`http://localhost:8081/waste/api/rewards/delete/${rewardId}`, {
      method: 'DELETE'
    })
            .then(response => response.json())
            .then(responseData => {
              if (responseData.success) {

                fetchRewards(); // 刷新奖励列表
              } else {

              }
            })
            .catch(error => {

            });
    alert('奖励物品删除成功！');
  }

  // 页面加载时调用
  window.onload = function() {
    fetchRewards();
  };
</script>
</body>
</html>